package com.teprinciple.wanandroid.ui.commom

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.teprinciple.wanandroid.ui.theme.blue1
import com.teprinciple.wanandroid.ui.theme.gray1

/**
 * 底部 Bar
 */
@Composable
fun BottomBar(tabs: List<BottomTab>, current: Int = 0, onTabClick: (Int) -> Unit) {
    Row(
        Modifier
            .height(50.dp)
            .fillMaxWidth()
            .background(Color.White)
    ) {
        tabs.forEachIndexed { index, tab ->
            BottomBarItem(
                tab = tab,
                selected = index == current,
                modifier = Modifier.weight(1f).clickable {
                    onTabClick.invoke(index)
                })
        }
    }
}

@Composable
fun BottomBarItem(tab: BottomTab, selected: Boolean, modifier: Modifier) {
    Column(
        modifier = Modifier.then(modifier).fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Icon(
            painter = painterResource(id = if (selected) tab.iconSelected else tab.iconNormal),
            contentDescription = tab.name,
            modifier = Modifier.size(20.dp),
            tint = if (selected) blue1 else gray1
        )

        Spacer(modifier = Modifier.height(2.dp))

        Text(
            text = tab.name,
            fontSize = 10.sp,
            color = if (selected) blue1 else gray1
        )
    }
}

data class BottomTab(
    val name: String,
    val iconNormal: Int,
    var iconSelected: Int = iconNormal,
)
